package com.sotb.sender;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
/**
 * 
 * @author lgb
 * 设置界面类
 */
public class SettingActivity extends Activity {
	private Button back_to_sender = null;
	private CheckBox record = null;
	private CheckBox experience = null;
	private final static String IS_RECORD = "isRecord";
	private final static String IS_EXPERIENCE = "isExperience";
	private final static String PREFERENCE_NAME = "set";
	boolean isRecord = false;
	boolean isExperience = true;
	SharedPreferences set = null;
	Editor editer = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_layout);
		//获得应用程序共享的preference
        set = getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE);
        //获得preference的editer
    	editer = set.edit();       
        experience = (CheckBox) findViewById(R.id.erperience);
        //读取设置
        isRecord = set.getBoolean(IS_RECORD, false);
        isExperience = set.getBoolean(IS_EXPERIENCE, true);
        back_to_sender = (Button) findViewById(R.id.back_to_sender);
        record = (CheckBox) findViewById(R.id.record);
        //读取checkBox设置
        record.setChecked(isRecord);
        experience.setChecked(isExperience);
        //是否录音checkBoc设置
        record.setOnClickListener(new OnClickListener() {			
			//@Override
			public void onClick(View v) {
				System.out.println(System.currentTimeMillis());
				// TODO Auto-generated method stub
				if (record.isChecked()){
					isRecord = true;					
				}else if(!record.isChecked()){
					isRecord = false;
				}
				editer.putBoolean(IS_RECORD, isRecord);
				System.out.println(System.currentTimeMillis());
			}
		});
        //是否体验checkBox设置
        experience.setOnClickListener(new OnClickListener() {
			
			//@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (experience.isChecked()){
					isExperience = true;					
				}else if(!experience.isChecked()){
					isExperience = false;
				}
				editer.putBoolean(IS_EXPERIENCE, isExperience);
			}
		});
        back_to_sender.setOnClickListener(new OnClickListener() {
			
			//@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(SettingActivity.this, SenderActivity.class);
				startActivity(intent);
			}
		});
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		if (editer != null){
			editer.commit();
		}			
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		//读取设置
        isRecord = set.getBoolean(IS_RECORD, false);
        isExperience = set.getBoolean(IS_EXPERIENCE, true);
        //读取checkBox设置
        record.setChecked(isRecord);
        experience.setChecked(isExperience);
		super.onResume();
	}
}
